package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description:
 * @Author raohui
 * @Date 2021/6/18 17:12
 */

@Service
public class IHomeServiceImpl implements IHomeService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    @Override
    public HomePageResponse homepage() {

        HomePageResponse homePageResponse = new HomePageResponse();
        try {
            //get请求没有参数，不需要进行参数校验,request.requestCheck()省略
            Example example = new Example(Panel.class);
            List<Panel> panels = panelMapper.selectByExample(example);
            Set<PanelDto> panelDtos = new HashSet<>();
            for (Panel panel : panels) {
                    PanelDto panelDto1 = contentConverter.panen2Dto(panel);
                    List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                    List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
                    panelDto1.setPanelContentItems(panelContentItemDtos);
                    panelDtos.add(panelDto1);
            }

            homePageResponse.setPanelContentItemDtos(panelDtos);
            homePageResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            homePageResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            return homePageResponse;
        } catch (Exception e) {
            homePageResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            homePageResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            return homePageResponse;
        }
    }


    @Override
    public HomePageResponse recommendHomepage() {

        HomePageResponse homePageResponse = new HomePageResponse();
        try {
            //get请求没有参数，不需要进行参数校验,request.requestCheck()省略
            Example example = new Example(Panel.class);
            List<Panel> panels = panelMapper.selectByExample(example);
            Set<PanelDto> panelDtos = new HashSet<>();
            for (Panel panel : panels) {
                PanelDto panelDto1 = contentConverter.panen2Dto(panel);
                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(6);
                List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
                panelDto1.setPanelContentItems(panelContentItemDtos);
                panelDtos.add(panelDto1);
            }

            homePageResponse.setPanelContentItemDtos(panelDtos);
            homePageResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            homePageResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            return homePageResponse;
        } catch (Exception e) {
            homePageResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            homePageResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            return homePageResponse;
        }
    }
}
